#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"

#include "7seg.h"
#include "core.h"
#include "globals.h"
#include "keypad.h"
#include "lcd.h"
#include "pwms.h"
#include "pwm_regs.h"
#include "pwm_routines.h"
#include "recording.h"

static char playback_mode = 0;
static char playback_started = 0;

int main(int argc, char ** argv)
{
    op_fnptr op_func = NULL;
    struct record_t * cur_record = NULL; 
    
    /* Initialise the outputs and PWMs */
    Clear7Seg();
    InitialiseLCD();
    WriteLCD("PRESS HARD TO\nPOWER DOWN");
    if (InitialisePWMs() != -1)
    {
        /* Ensure the arm starts in a known position */
        HomePWMs();
        
        LOOP_FOREVER()
        {            
            /* Set keypad button press flags */
            if (ReadKeypad() == KEYPAD_BUTTON_PRESSED)
            {
                if (!(playback_mode))
                {
                    if (PlaybackKey())
                    {
                        playback_mode = 1;
                    }
                    
                    /* Translate keypad input into operation */
                    op_func = GetOpFromKeypadFlags();
                    if (op_func != NULL)
                    {
                        op_func();
                        op_func = NULL;
                    }
                    else
                    {
                        /* TODO: Error - handle it */
                        DisablePWMs();
                        WriteLCD("ERROR\nUnknown operation");
                        Write7Seg("E  0");
                        return 1;
                    }
                }
                else
                {
                    playback_mode = 0;
                }
            }
            else
            {
                if (!(playback_mode))
                {
                    /* Null action */
                    if (RecordingMode())
                    {
                        RecordEvent(EVENT_STOP, 1);
                    }
                    usleep(1000);
                    
                    Write7Seg("NOIN");
                    WriteLCD("-- IDLE\n--");
                }
                else
                {
                    if (playback_started)
                    {
                        cur_record = cur_record->next_record;
                    }
                    else
                    {
                        cur_record = GetFirstRecord();
                        playback_started = 1;
                    }
                    
                    if (cur_record)
                    {
                        op_func = GetOpFromEvtCode(cur_record->event_code);
                        if (op_func)
                        {
                            op_func();
                            usleep(1000);
                        }
                        else
                        {
                            playback_mode = 0;
                            playback_started = 0;
                        }
                    }
                    else
                    {
                        playback_mode = 0;
                        playback_started = 0;
                    }
                }
            }
            
            usleep(1000);
        }
    }
    else
    {
        WriteLCD("Error: Unable to initialise PWMs\n");
        Write7Seg("E  1");
    }
    
    return 0;
}
